package com.ruoyi.web.controller.lwpq;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.lwpq.domain.LwpqJl;
import com.ruoyi.web.controller.lwpq.LwpqJlController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lwpq.domain.LwpqYggl;
import com.ruoyi.lwpq.service.ILwpqYgglService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 单位用工管理Controller
 * 
 * @author ruoyi
 * @date 2022-03-03
 */
@Controller
@RequestMapping("/lwpq/yggl")
public class LwpqYgglController extends BaseController
{
    private String prefix = "lwpq/yggl";
    private static final Logger log = LoggerFactory.getLogger(LwpqYgglController.class);

    @Autowired
    private ILwpqYgglService lwpqYgglService;

    //    此处可以直接由前端传入头像地址,例如下方
    @GetMapping("/avatar")
    public String avatar(@RequestParam(required = false) String url,ModelMap mmap)
    {
        if (StringUtils.isNotEmpty(url)){
            mmap.put("avatar",url);
        }
        return prefix + "/avatar";
    }

    /**
     * 保存头像
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
                return success(avatar);
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("修改头像失败！", e);
            return error(e.getMessage());
        }
    }

    @RequiresPermissions("lwpq:yggl:view")
    @GetMapping()
    public String yggl()
    {
        return prefix + "/yggl";
    }

    /**
     * 查询单位用工管理列表
     */
    @RequiresPermissions("lwpq:yggl:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LwpqYggl lwpqYggl)
    {
        startPage();
        lwpqYggl.setCreateBy(getLoginName());
        List<LwpqYggl> list = lwpqYgglService.selectLwpqYgglList(lwpqYggl);
        return getDataTable(list);
    }

    /**
     * 导出单位用工管理列表
     */
    @RequiresPermissions("lwpq:yggl:export")
    @Log(title = "单位用工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LwpqYggl lwpqYggl)
    {
        List<LwpqYggl> list = lwpqYgglService.selectLwpqYgglList(lwpqYggl);
        ExcelUtil<LwpqYggl> util = new ExcelUtil<LwpqYggl>(LwpqYggl.class);
        return util.exportExcel(list, "单位用工管理数据");
    }

    /**
     * 新增单位用工管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存单位用工管理
     */
    @RequiresPermissions("lwpq:yggl:add")
    @Log(title = "单位用工管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LwpqYggl lwpqYggl)
    {
        lwpqYggl.setCreateBy(getLoginName());
        return toAjax(lwpqYgglService.insertLwpqYggl(lwpqYggl));
    }

    /**
     * 修改单位用工管理
     */
    @RequiresPermissions("lwpq:yggl:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        LwpqYggl lwpqYggl = lwpqYgglService.selectLwpqYgglById(id);
        mmap.put("lwpqYggl", lwpqYggl);
        return prefix + "/edit";
    }

    /**
     * 修改保存单位用工管理
     */
    @RequiresPermissions("lwpq:yggl:edit")
    @Log(title = "单位用工管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LwpqYggl lwpqYggl)
    {
        lwpqYggl.setUpdateBy(getLoginName());
        return toAjax(lwpqYgglService.updateLwpqYggl(lwpqYggl));
    }

    /**
     * 删除单位用工管理
     */
    @RequiresPermissions("lwpq:yggl:remove")
    @Log(title = "单位用工管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lwpqYgglService.deleteLwpqYgglByIds(ids));
    }


    @RequiresPermissions("lwpq:yggl:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LwpqYggl> util = new ExcelUtil<LwpqYggl>(LwpqYggl.class);
        List<LwpqYggl> lwpqyggl = util.importExcel(file.getInputStream());
        String message = lwpqYgglService.importYggl(lwpqyggl, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("lwpq:yggl:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<LwpqYggl> util = new ExcelUtil<LwpqYggl>(LwpqYggl.class);
        return util.importTemplateExcel("用工数据");
    }
}
